import { ECSComponentSetImpl } from "./component/ECSComponentSetImpl";
import { ECSComponentSet } from "./component/ECSComponentSet";
import { ECSEntitySetImpl } from "./entity/ECSEntitySetImpl";
import { ECSEntitySet } from "./entity/ECSEntitySet";
import { ECSEventSet } from "./event/ECSEventSet";
import { ECSSystemSet } from "./system/ECSSystemSet";
import { ECSSystemSetImpl } from "./system/ECSSystemSetImpl";
import { ECSWorld } from "./ECSWorld";
import { ECSEventSetImpl } from "./event/ECSEventSetImpl";


export class ECSWorldImpl implements ECSWorld {

    // 事件集合
    readonly events: ECSEventSet;

    // 实体集合
    readonly entities: ECSEntitySet;

    // 组件集合
    readonly components: ECSComponentSet;

    // 系统集合
    readonly systems: ECSSystemSet;

    private _systemsImpl: ECSSystemSetImpl;

    private _isStart: boolean = false;

    constructor() {
        this.events     = new ECSEventSetImpl();
        this.components = new ECSComponentSetImpl(this);
        this.entities   = new ECSEntitySetImpl(this);
        this.systems    = this._systemsImpl = new ECSSystemSetImpl(this);
    }

    start(): void {
        if (!this._isStart) {
            this._isStart = true;
            this._systemsImpl.start();
        }
    }

    stop(): void {
        if (this._isStart) {
            this._isStart = false;
            this._systemsImpl.stop();
        }
    }

    update(dt: number): void {
        if (this._isStart) {
            this._systemsImpl.update(dt);
            this.events.clear();
        } 
    }
}
